package com.smedi.ismedi.notification.core.port.adapter.restful;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smedi.ismedi.notification.api.dto.request.SmsTemplateRequest;
import com.smedi.ismedi.notification.api.dto.response.SmsTemplateResponse;
import com.smedi.ismedi.notification.api.feign.RemoteSmsTemplateFeignApi;
import com.smedi.ismedi.notification.core.application.command.SmsTemplateCommand;
import com.smedi.ismedi.notification.core.application.query.SmsTemplateQueryService;
import com.smedi.ismedi.notification.core.application.query.vo.SmsTemplateVo;
import com.smedi.ismedi.notification.core.application.service.SmsTemplateApplicationService;
import com.smedi.ismedi.notification.core.domain.aggregatemodel.SmsTemplate;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: xulinjun
 * @Date: 2021-7-11 15:21
 */
@Slf4j
@RestController
@AllArgsConstructor
public class SmsTemplateController implements RemoteSmsTemplateFeignApi {
    private final DozerUtil dozerUtil;

    private final SmsTemplateApplicationService smsTemplateApplicationService;

    private final SmsTemplateQueryService smsTemplateQueryService;

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<SmsTemplateResponse> postSaveSmsTemplate(SmsTemplateRequest smsTemplateRequest) {
        SmsTemplateCommand smsTemplateCommand = dozerUtil.convertor(smsTemplateRequest, SmsTemplateCommand.class);
        SmsTemplate smsTemplate = smsTemplateApplicationService.saveTemplate(smsTemplateCommand);

        return new ResponseData<SmsTemplateResponse>().success(dozerUtil.convertor(smsTemplate, SmsTemplateResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> deleteSmsTemplate(Long smsTemplateId) {
        smsTemplateApplicationService.deleteTemplate(smsTemplateId);
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<PaginationData<SmsTemplateResponse>> getQueryTemplateTable(Long offset, Long pageSize, String templateName) {
        IPage pagination = smsTemplateQueryService.getSmsTemplatePaginationByTemplateName(offset, pageSize, templateName);
        pagination.setRecords(dozerUtil.convertor(pagination.getRecords(), SmsTemplateResponse.class));

        PaginationData PaginationData = dozerUtil.convertor(pagination, PaginationData.class);
        return new ResponseData<PaginationData<SmsTemplateResponse>>().success(PaginationData);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<SmsTemplateResponse> getQuerySmsTemplate(Long smsTemplateId) {
        SmsTemplateVo smsTemplateVo = smsTemplateQueryService.getSmsTemplateBySmsTemplateId(smsTemplateId);
        return new ResponseData<SmsTemplateResponse>().success(dozerUtil.convertor(smsTemplateVo, SmsTemplateResponse.class));
    }
}
